<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MailingPage
 *
 * @author juwking
 */
class MailingPage extends PageController {
    const messagePath="../messages/";

    function __construct() {
        parent::__construct();
    }

    function getPageName() {
        return 'mailing';
    }

    public function defaultAction() {
        $this->display("mailing/mailingMain.tpl");
    }

    public function getSidebar() {
        $sidebar = array(
            array(label => 'Przygotuj wiadomość', link => 'createMailing'),
            array(label => 'Przygotuj kolejke', link => 'createQue')
        );
        return $sidebar;
    }

    public function createMailing() {
        $this->smarty->display('mailing/mailingTinyTCE.tpl');
    }

    public function createQue() {
        $message = MessageQuery::create()->setFormatter(ModelCriteria::FORMAT_ARRAY)->find();

        $this->assign('content', 'mailing/queCreator.tpl');
        foreach ($message as $key => $value) {
            $messageArray[$key] = $value;
            #$messageArray[$key]['message_content'] = $this->readMessage($value->getMessagePath());
            $messageArray[$key]['message_content'] = $this->readMessage(MailingPage::messagePath . $value['MessagePath']);
        }
        $this->assign('messages', $messageArray);
        $this->smarty->display('mailing/queCreator.tpl');
    }

    public function createQue2($page=null) {
        $this->display("grid/client.tpl");
    }

    public function createQue3() {
        if (isset($_SESSION['createdMessage'])) {
            $_SESSION['createdMessage']->evaluate();
            $this->assign('message', $_SESSION['createdMessage']);
        }
        $this->display('mailing/summary.tpl');
        // var_dump($m->getMessageContent());
    }

    public function createQueFinal() {
        if (isset($_SESSION['createdMessage']))
            $_SESSION['createdMessage']->makeQue();
        unset( $_SESSION['createdMessage']);
        $this->defaultAction();
    }
    public function getTargetNum(){
        echo $_SESSION['createdMessage']->getTargetNum();
    }
    public function updateTarget() {
        $_SESSION['createdMessage']->updateTarget($_REQUEST['Target']);
    }

    public function showClientGrid() {
        include_once '../components/DataGrid/clientformailing.php';
    }

    public function prepareMessage() {
        $message = MessageQuery::create()->filterByMessageId($_REQUEST['MessageId'])->findOne();

        $_SESSION['createdMessage'] = new MailingQueClass($message);
    }

    private function readMessage($filename) {
        $fh = fopen($filename, 'r');
        $theData = fread($fh, filesize($filename));
        fclose($fh);
        return $theData;
    }

    public function saveMessage() {
        $filename = "../messages/" . rand() . '.txt';
        $fp = fopen($filename, 'w');
        fwrite($fp, $_REQUEST['message']);
        fclose($fp);
        $message = new MessageQuery();

        $message->Insert(array('message_users_id' => $_SESSION['UserAccess']["users_id"], 'message_path' => $filename, 'message_mail_type_id' => 1));
        $this->assign('content', $this->getContent());
    }

}

?>
